﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace NFinal.Middleware.Resources.Helper
{
    public class ResourcesXmlConfig
    {
        private string fileName;
        public ResourcesXmlConfig(string fileName)
        {
            this.fileName = fileName;
            XmlDocument xmlDocument = new XmlDocument();
#if NETCORE
            xmlDocument.Load(File.OpenText(fileName));
#else
            xmlDocument.Load(fileName);
#endif
            XmlNodeList textResourceNodeList = xmlDocument.DocumentElement.SelectNodes("textResource");
            this.textResources = new TextResource[textResourceNodeList.Count];
            TextResource textResource;
            for(int i=0;i<textResourceNodeList.Count;i++)
            {
                textResource = new TextResource();
                textResource.fileName= textResourceNodeList[i].Attributes["fileName"].Value;
                textResource.extensions = textResourceNodeList[i].Attributes["extension"].Value.Split('|');
                textResource.includes = GetInclude(textResourceNodeList[i]);
                textResource.excludes = GetExclude(textResourceNodeList[i]);
                this.textResources[i] = textResource;
            }
            XmlNodeList streamResourceNodeList = xmlDocument.DocumentElement.SelectNodes("streamResource");
            this.streamResources = new StreamResource[streamResourceNodeList.Count];
            StreamResource streamResource;
            for (int i = 0; i < streamResourceNodeList.Count; i++)
            {
                streamResource = new StreamResource();
                streamResource.fileName = streamResourceNodeList[i].Attributes["fileName"].Value;
                streamResource.extensions = streamResourceNodeList[i].Attributes["extension"].Value.Split('|');
                streamResource.includes = GetInclude(streamResourceNodeList[i]);
                streamResource.excludes = GetExclude(streamResourceNodeList[i]);
                this.streamResources[i] = streamResource;
            }
            XmlNodeList mimeXmlNodeList = xmlDocument.DocumentElement.SelectNodes("staticContent/mimeMap");
            staticContent = new StaticContent();
            if (mimeXmlNodeList.Count > 0)
            {
                staticContent.mimeMaps = new MimeMap[mimeXmlNodeList.Count];
            }
            else
            {
                staticContent.mimeMaps = new MimeMap[0];
            }
            for (int i = 0; i < mimeXmlNodeList.Count; i++)
            {
                staticContent.mimeMaps[i] = new MimeMap();
                staticContent.mimeMaps[i].fileExtension = mimeXmlNodeList[i].Attributes["fileExtension"].Value;
                staticContent.mimeMaps[i].mimeType = mimeXmlNodeList[i].Attributes["mimeType"].Value;
            }
        }
        private Include[] GetInclude(XmlNode xmlNode)
        {
            Include[] includes;
            XmlNodeList includeNodeList = xmlNode.SelectNodes("include");
            if (includeNodeList.Count > 0)
            {
                includes = new Include[includeNodeList.Count];
            }
            else
            {
                includes = new Include[0];
            }
            Include include;
            for (var i = 0; i < includeNodeList.Count; i++)
            {
                include = new Include();
                include.requestPath = includeNodeList[i].Attributes["requestPath"].Value;
                include.localPath = includeNodeList[i].Attributes["localPath"].Value;
                includes[i] = include;
            }
            return includes;
        }
        private Exclude[] GetExclude(XmlNode xmlNode)
        {
            Exclude[] excludes;
            XmlNodeList excludeNodeList = xmlNode.SelectNodes("exclude");
            if (excludeNodeList.Count > 0)
            {
                excludes = new Exclude[excludeNodeList.Count];
            }
            else
            {
                excludes = new Exclude[0];
            }
            Exclude exclude;
            for (var i = 0; i < excludeNodeList.Count; i++)
            {
                exclude = new Exclude();
                exclude.localPath = excludeNodeList[i].Attributes["localPath"].Value;
                excludes[i] = exclude;
            }
            return excludes;
        }
        public TextResource[] textResources;
        public StreamResource[] streamResources;
        public StaticContent staticContent;
    }
    public class TextResource
    {
        public string fileName;
        public string[] extensions;
        public Include[] includes;
        public Exclude[] excludes;
    }
    public class StreamResource
    {
        public string fileName;
        public string[] extensions;
        public Include[] includes;
        public Exclude[] excludes;
    }
    public class Include
    {
        public string requestPath;
        public string localPath;
    }
    public class Exclude
    {
        public string localPath;
    }
    public class StaticContent
    {
        public MimeMap[] mimeMaps;
    }
    public class MimeMap
    {
        public string fileExtension;
        public string mimeType;
    }
}
